package com.wh.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.basic.vo.AjaxResult;
import com.wh.org.pojo.Employee;
import com.wh.system.dao.PermissionDao;
import com.wh.system.dao.RoleDao;
import com.wh.system.dao.RoleMenuDao;
import com.wh.system.dao.RolePermissionDao;
import com.wh.system.dto.RoleDto;
import com.wh.system.pojo.Role;
import com.wh.system.pojo.RoleMenu;
import com.wh.system.pojo.RolePermission;
import com.wh.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceimpl implements IRoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleMenuDao roleMenuDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Transactional
    @Override
    public void add(Role role) {
        //添加角色
        roleDao.insert(role);
        //添加角色对应权限
        if (ObjectUtil.isNotNull(role.getPermissionIds())) {
            List<Long> permissionIds = role.getPermissionIds();
            for (Long permissionId : permissionIds) {
                rolePermissionDao.insert(new RolePermission().setRoleId(role.getId()).setPermissionId(permissionId));
            }
        }
        //3.添加角色对应菜单
        if (ObjectUtil.isNotEmpty(role.getMenuIds())) {
            List<Long> menuIds = role.getMenuIds();
            for (Long menuId : menuIds) {
                roleMenuDao.insert(new RoleMenu().setRoleId(role.getId()).setMenuId(menuId));
            }
        }

    }

    @Override
    public void del(Long id) {
        roleDao.deleteById(id);
    }

    @Override
    public void upd(Role role) {
        //1.修改角色
        roleDao.updateById(role);
        //2.修改角色对应权限
        UpdateWrapper<RolePermission> rmUw = new UpdateWrapper<>();
        rmUw.ge("role_id", role.getId());
        rolePermissionDao.delete(rmUw);
        if (ObjectUtil.isNotEmpty(role.getPermissionIds())) {
            List<Long> permissionIds = role.getPermissionIds();
            for (Long permissionId : permissionIds) {
                rolePermissionDao.insert(new RolePermission().setRoleId(role.getId()).setPermissionId(permissionId));
            }
        }
        //3.修改角色对应菜单
        UpdateWrapper<RoleMenu> rpUw = new UpdateWrapper<>();
        rpUw.ge("role_id", role.getId());
        roleMenuDao.delete(rpUw);
        if (ObjectUtil.isNotEmpty(role.getMenuIds())) {
            List<Long> menuIds = role.getMenuIds();
            for (Long menuId : menuIds) {
                roleMenuDao.insert(new RoleMenu().setRoleId(role.getId()).setMenuId(menuId));
            }
        }

    }

    @Override
    public List<Role> findAll() {
        return roleDao.selectList(null);
    }

    @Override
    public Role findOne(Long id) {
        return roleDao.selectById(id);
    }




    @Transactional
    @Override
    public void patchDel(List<Long> ids) {
        //1.删除角色对应的菜单
        UpdateWrapper<RoleMenu> rmUw = new UpdateWrapper<>();
        rmUw.in("role_id", ids);
        roleMenuDao.delete(rmUw);
        //2.删除角色对应的权限
        UpdateWrapper<RolePermission> rpUw = new UpdateWrapper<>();
        rpUw.in("role_id", ids);
        rolePermissionDao.delete(rpUw);
        //3.删除角色
        roleDao.deleteBatchIds(ids);
    }

    @Override
    public IPage<Role> findByPage(RoleDto roleDto) {
        //1.创建查询条件
        QueryWrapper<Role> qw = new QueryWrapper<>();
//            qw.like("xxx",roleDto.getKeyword());
//            qw.or();
//            qw.like("xxx",roleDto.getKeyword());
        //2.创建分页参数

        IPage<Role> page = new Page<>(roleDto.getCurrentPage(),roleDto.getPageSize());
        //3.查询数据
        page.setRecords(roleDao.findByPage(page, qw));
        page.setTotal(roleDao.selectCount(qw));
        //4.返回结果
        return page;
    }
    @Transactional
    @Override
    public AjaxResult updatemenu(Role role) {

        Long id = role.getId();//角色id
        //前端可能修改角色名
        roleDao.updateById(role);
        RolePermission rolePermission=new RolePermission();
        RoleMenu roleMenu= new RoleMenu();

        //先删除角色id所拥有的菜单和权限数据
        roleMenuDao.deleteByRoleId(id);
        rolePermissionDao.deleteByRoleId(id);
        rolePermission.setRoleId(id);
        roleMenu.setRoleId(id);
        List<Long> selectedMenuId = role.getSelectedMenuId();//前端传递的菜单数据
        Set<Long> uniqueMenuIds = new LinkedHashSet<>(selectedMenuId);
        for (Long aLong : uniqueMenuIds) {
            roleMenu.setMenuId(aLong);
            roleMenuDao.insert(roleMenu);
        }
        List<Long> selectedPressionId = role.getSelectedPressionId();//前端传递的权限数据
        Set<Long> uniquePressionIds = new LinkedHashSet<>(selectedPressionId);
        for (Long aLong : uniquePressionIds) {
            rolePermission.setPermissionId(aLong);
            rolePermissionDao.insert(rolePermission);
        }
        return AjaxResult.me();
    }
}


